from __future__ import with_statement

import bisect

import scipy

import common

float_type = float

if scipy.version.version < '0.7.0':
    kb_t = 1.3806505000000001e-23 * float_type(301.15)
else:
    import scipy.constants
    kb_t = float_type(scipy.constants.k) * float_type(301.15)

p = float_type(0.35e-9)
kb_t_over_p = kb_t / p

gb1_kf0 = 720
gb1_alpha = 2.1e-9 / kb_t

gb1_8_folded_length = 32e-9
gb1_loop_length = 18e-9
gb1_8_unfolded_length = gb1_8_folded_length + 8 * gb1_loop_length

gb1_folded_length = gb1_8_folded_length / 8.0
gb1_unfolded_length = gb1_8_unfolded_length / 8.0

zero = float_type(0)


def data_filename(filename):
    return filename.rstrip("*").rstrip("_break")

def read_entries(filename):
    """
    To add quotation marks, Query replace regexp (default  \([^ ]+_break\*?\) ->  "\1"):
    """
    print(filename)
    entries = []
    with open(filename) as f:
        for line in f:
            if not line.startswith("#"):
                common.check_False(line.startswith("k="))
                if line.strip():
                    tpl = eval(line)
                    entries.append(tpl)
    return entries

def gradient(y):
    sorted_list = sorted(set(y))
    colours = [(1 - 2 * p if p < 0.5 else 0, 2 * p if p < 0.5 else 2 * (1 - p), 0 if p < 0.5 else 1 - 2 * (1 - p)) for p in [
                bisect.bisect_left(sorted_list, yy) / float(len(sorted_list) - 1) if len(sorted_list) != 1 else 0.5 for yy in y]]
    return colours
